package com.th.supcom.pds.drugreturn.client.widget;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.th.supcom.pds.internal.client.model.MedicareCardAllInfoModel;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.widget.MedicareCardField;
import com.th.supcom.pds.recipefee.client.widget.RecipeFeeUtils;

/**
 * 门诊退费申请主面板.
 * 
 * @author yzhang
 */
public class RefundApplicationMainPanel extends ContentPanel
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    /** 门诊退费申请文本域集合. */
    private DrugReturnAndRefundFields m_drugReturnAndRefundFields;

    /** 门诊退费申请处方信息面板. */
    private DrugReturnAndRefundPrescPanel m_drugReturnAndRefundPrescPanel;

    /** 门诊退费申请Action */
    private RefundApplicationAction m_refundApplicationAction;

    /** 门诊退费申请按钮集合. */
    private RefundApplicationButtons m_refundApplicationButtons;

    /** 门诊退费申请处方明细信息面板. */
    private RefundApplicationPrescDetailPanel m_refundApplicationPrescDetailPanel;
    /** 医保卡刷卡. */
    private MedicareCardField m_medicareCardField;

    /**
     * 添加刷卡组件
     */
    private void addMedicareCard ()
    {
        m_medicareCardField = new MedicareCardField (this)
        {

            @Override
            public void handleEventAfter ()
            {
                /*PatientInfoModel model = RecipeFeeUtils.initPatientInfo (m_medicareCardField);
                m_refundApplicationAction.initPatiInfo (model);
                m_refundApplicationAction.queryByPid (model.getPatiId (), null);
                return;*/
            }
        };
    }

    public RefundApplicationMainPanel ()
    {
        initUI ();
        initAction ();
        initListeners ();

    }

    /**
     * 初始化事件
     */
    private void initAction ()
    {
        m_refundApplicationAction = new RefundApplicationAction ();
        m_refundApplicationAction.setDrugReturnAndRefundFields (m_drugReturnAndRefundFields);
        m_refundApplicationAction.setDrugReturnAndRefundPrescPanel (m_drugReturnAndRefundPrescPanel);
        m_refundApplicationAction.setRefundApplicationPrescDetailPanel (m_refundApplicationPrescDetailPanel);

    }

    /**
     * 初始化事件监听
     */
    private void initListeners ()
    {
        /** 门诊退费申请Listener */
        final RefundApplicationListener listener = new RefundApplicationListener ();
        listener.setDrugReturnAndRefundFields (m_drugReturnAndRefundFields);
        listener.setRefundApplicationAction (m_refundApplicationAction);
        listener.setRefundApplicationButtons (m_refundApplicationButtons);
        listener.setDrugReturnAndRefundPrescPanel (m_drugReturnAndRefundPrescPanel);
        listener.setKeyNavComponent (this);
        listener.initListeners ();
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        /** 退费申请查询信息 面板 */
        RefundApplicationInfoPanel m_refundApplicationInfoPanel;
        setHeaderVisible (false);
        setLayout (new BorderLayout ());
        // 初始化对象
        m_refundApplicationButtons = new RefundApplicationButtons ();
        m_drugReturnAndRefundFields = new DrugReturnAndRefundFields ();
        addMedicareCard ();
        m_medicareCardField.setFieldLabel ("刷卡");
        m_drugReturnAndRefundFields.setMedicareCardField (m_medicareCardField);
        m_refundApplicationInfoPanel = new RefundApplicationInfoPanel (m_drugReturnAndRefundFields, m_refundApplicationButtons, m_medicareCardField);
        final RefundApplicationPatientInfoPanel refundPatInfoPanel = new RefundApplicationPatientInfoPanel (m_drugReturnAndRefundFields);
        m_refundApplicationPrescDetailPanel = new RefundApplicationPrescDetailPanel ();
        m_drugReturnAndRefundPrescPanel = new DrugReturnAndRefundPrescPanel ();
        m_refundApplicationAction = new RefundApplicationAction ();

        final LayoutContainer northPanelContainer = new LayoutContainer ();
        northPanelContainer.setLayout (new RowLayout (Orientation.VERTICAL));

        northPanelContainer.add (m_refundApplicationInfoPanel, new RowData (1, 0.5, new Margins (0)));
        northPanelContainer.add (refundPatInfoPanel, new RowData (1, 0.5, new Margins (0)));
        // 设置布局和大小
        final BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH, 180);
        northData.setMargins (new Margins (4));
        final BorderLayoutData westData = new BorderLayoutData (LayoutRegion.WEST, 272);
        westData.setMargins (new Margins (0, 4, 4, 4));
        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (0, 4, 4, 0));

        add (northPanelContainer, northData);
        add (m_drugReturnAndRefundPrescPanel, westData);
        add (m_refundApplicationPrescDetailPanel, centerData);

        // 初始化下部按钮
        final ContentPanel bottomContainer = new ContentPanel ();
        bottomContainer.setHeaderVisible (false);
        bottomContainer.setButtonAlign (HorizontalAlignment.CENTER);
        bottomContainer.addButton (m_refundApplicationButtons.getRefundApplicationBtn ());
        bottomContainer.addButton (m_refundApplicationButtons.getClearBtn ());
        setBottomComponent (bottomContainer);
        m_drugReturnAndRefundFields.getMedicareCardField ().focus ();
    }
}